<template>
<view>
	<scroll-view scroll-y="true" style="height: 80vh;">
		<view class="form_list">
			<view class="label">单号</view>
			<view class="input">
				<uni-easyinput v-model="FBillNo" v-if="!show_FBillNo_FBillNopda" :placeholderStyle="placeholderStyle" placeholder="请输入内容"></uni-easyinput>
				<uni-easyinput v-model="FBillNoPda" v-if="show_FBillNo_FBillNopda" :placeholderStyle="placeholderStyle" placeholder="请输入内容"></uni-easyinput>
			</view>
		</view>
		<view class="form_list">
			<view class="label">单据日期</view>
			<view class="input">
				<uni-datetime-picker type="date" name="FDate" v-model="FDateVal" class="border" />
			</view>
		</view>
		<view class="form_list" v-if="prestr=='CKKC'?true:false">
			<view class="label">账面日期</view>
			<view class="input">
				<uni-datetime-picker type="date" name="bookFDate" v-model="bookFDateVal" class="border" />
			</view>
		</view>
		
		<view v-if="sel_source"> 
			<view class="form_list">
				<view class="label">源单</view>
				<view class="input">
					<uni-easyinput :placeholderStyle="placeholderStyle"
						:value="poorder.pcode" disabled v-if="!show_FBillNo_FBillNopda">
					</uni-easyinput>
					<uni-easyinput :placeholderStyle="placeholderStyle"
						:value="poorder.FBillNo" disabled v-if="show_FBillNo_FBillNopda">
					</uni-easyinput>
				</view>
			</view>
			<view class="form_list" v-show="show_in_factory">
				<view class="label">供应商</view>
				<view class="input">
					<uni-easyinput :placeholderStyle="placeholderStyle" 
						:value="poorder.factory_name" disabled></uni-easyinput>
				</view>
			</view>
			<view class="form_list" v-show="show_out_dpt">
				<view class="label">部门</view>
				<view class="input">
					<uni-easyinput :placeholderStyle="placeholderStyle" 
						:value="poorder.cdepname" disabled></uni-easyinput>
				</view>
			</view>
			<view class="form_list" v-show="order_type==5?true:false">
				<view class="label">客户</view>
				<view class="input">
					<uni-easyinput :placeholderStyle="placeholderStyle" 
						:value="poorder.cUser_Name" disabled></uni-easyinput>
				</view>
			</view>
		</view>
		
		<view class="form_list" v-show="order_type==14?true:false">
			<view class="label">供应商</view>
			<view class="input">
				<view class="input">
					<uni-easyinput v-model="supplier.fName" prefixIcon="search" @iconClick="toselect('supplier')" disabled style="width:85%;float:left"></uni-easyinput>
					<uni-icons custom-prefix="custom-icon" type="close" size="25" @click="clearSelect()"></uni-icons>
				</view> 
			</view>
		</view>
		
		<view class="form_list" v-show="order_type==11||order_type==12||order_type==15||order_type==19?true:false">
			<view class="label">部门</view>
			<view class="input">
				<uni-easyinput v-model="department.fName" prefixIcon="search" @iconClick="toselect('department')" disabled style="width:85%;float:left"></uni-easyinput>
				<uni-icons custom-prefix="custom-icon" type="close" size="25" @click="clearSelect()"></uni-icons>
			</view>
		</view>
		
		<view class="form_list">
			<view class="label">所在仓库</view>
			<view class="input">
				<!-- 仓库组件 -->
				<stockSelect :stock_id.sync="stock_id" :disabled="stkDisabled"></stockSelect>
			</view>
		</view>
		
		<view class="form_list" v-if="isallot">
			<view class="label">目标仓库</view>
			<view class="input">
				<!-- 仓库组件 -->
				<stockSelect :stock_id.sync="to_stock_id" :isallot="isallot" :disabled="stkDisabled"></stockSelect>
			</view>
		</view>
		
		<!-- 调拨1次性选择目标库区位置 -->
		<view class="form_list" v-if="order_type==16?true:false">
			<view class="label">目标库区</view>
			<view class="input">
				<uni-easyinput v-model="to_shelves_name" prefixIcon="search" @iconClick="toGetShelves()" disabled style="width:85%;float:left"></uni-easyinput>
				<uni-icons custom-prefix="custom-icon" type="close" size="25" @click="clearShelves()"></uni-icons>
			</view> 
		</view>
		
		<!-- <view class="form_list" v-if="order_type==5?true:false">
			<view class="label">流水号</view>
			<view class="input">
				<uni-easyinput type="textarea" :placeholderStyle="placeholderStyle" autoHeight 
				v-model="qrNoStr" placeholder="无需录入,扫码自动识别"></uni-easyinput>
			</view> 
		</view> -->
		
		
		<view class="form_list">
			<view class="label">摘要</view>
			<view class="input">
				<uni-easyinput type="textarea" :placeholderStyle="placeholderStyle"
					autoHeight v-model="remark" placeholder="请输入内容"></uni-easyinput>
			</view>
		</view>
	</scroll-view>
	<view class="swiper-button" v-if="prestr!='CKKC'&&prestr!='DB'?true:false">
		<button @click="toSerial" v-if="sel_source">选择源单</button>
		<button @click="toOperlog" class="ok_btn">操作记录</button>
		<button @click="toListFh" v-if="showCheckBtn" class="del_btn">复核列表</button>
		<view style="width: 220upx" v-if="!sel_source"></view>
	</view>
	<scanCode></scanCode>
</view>	
</template>

<script>
	import stockSelect from "@/component/stockhouse.vue"
	import scanCode from "@/component/scan-code.vue"
	import {
	  uniEasyinput,uniDatetimePicker,uniIcons
	} from "@dcloudio/uni-ui"
	
	export default {
		props: {
		    prestr: { //前缀
		        type: String,
		        default: 'IN'
		    },
			order_type:{
				//type:Number,
				default:0
			},
			poorder: { //源单
			    type: Object,
			    default: function() {
				  return {};
				}
			},
			sel_source:{
				type:Boolean,
				default:true
			},
			isallot:{
				type:Boolean,
				default:false
			},
			stkDisabled:{
				type:Boolean,
				default:false
			},
			showCheckBtn:{
				type:Boolean,
				default:false
			}
		},
		components: {
			uniEasyinput,uniDatetimePicker,
			uniIcons,stockSelect,scanCode
		},
		data() {
		  return {
			  placeholderStyle:'font-size:17px',
			  
			  FDateVal:'', //单据日期
			  bookFDateVal:'',//账面日期，盘点专用
			  FBillNo: '', //单号
			  FBillNoPda:'',//上架、出库单号，用到，上面那个当源单
			  stock_id:0,  //所在仓库id
			  to_stock_id:0, //目标仓库id 用于调拨
			  today:'',
			  remark:'',
			  qrNoStr:'',
			  
			  supplier:{},
			  department:{},
			  
			  to_shelves_name:'',
			  to_shelves_id:'',
			  
			  in_order_type_arr:[0,1,14],
			  out_order_type_arr:[2,3,15],
			  show_in_factory:false,//是否显示供应商
			  show_out_dpt:false,//是否显示部门
			  show_FBillNo_FBillNopda:false,
			  user:uni.getStorageSync('user'),
			  myconfig:uni.getStorageSync('myconfig')
			}
		},
		mounted(){
			this.show_in_factory = this.$Func.inArray(this.order_type,this.in_order_type_arr)
			this.show_out_dpt = this.$Func.inArray(this.order_type,this.out_order_type_arr)
			if(this.prestr=='IN'||this.prestr=='OUT'){
				//上架、出库 因引用本地单据，所以参数得变
				this.show_FBillNo_FBillNopda = true
			}
			
			uni.$off("getShelvesHead")
			uni.$on("getShelvesHead", (shelves) => {
				this.to_shelves_id = shelves.id
				this.to_shelves_name = shelves.name
			})
			
			if(this.order_type==14){
				uni.$off("getSupplier")
				uni.$on("getSupplier", (supplier) => {
					this.supplier = supplier
				})
			}else{
				uni.$off("getSupplier")
			}
			
			if(this.order_type==11||this.order_type==12||this.order_type==15||this.order_type==19){
				uni.$off("getDepartment")
				uni.$on("getDepartment", (department) => {
					this.department = department
				})
			}else{
				uni.$off("getDepartment")
			}
		},
		watch:{
			//监听表头仓库变化
			stock_id(newval,oldval){
				uni.$emit('select_stock_id',newval)
				uni.$emit('sel_stock_id',newval)
			},
			to_stock_id(newval,oldval){
				uni.$emit('sel_to_stock_id',newval)
			},
		},
		methods:{
			init(){
				this.today = this.$Func.getDateTime('today','Ymd');
				//渲染pda单号
				this.$Db.setMcode(this.today, this.prestr).then(res => {
					if(this.prestr=='IN'||this.prestr=='OUT'){
						this.FBillNoPda = res;
					}else{
						this.FBillNo = res;
					}
				});
				//渲染仓库
				let _this = this
				this.stock_id = this.user.stock_id
				this.to_stock_id = this.user.stock_id //调拨专用，默认渲染
				
				this.remark = ''
				this.qrNoStr = ''
				//this.qrNoArr = []
				
				//日期重置
				this.FDateVal = this.$Func.getDateTime('today');
				this.bookFDateVal = this.$Func.getDateTime('today');
				
				//默认部门
				if(this.myconfig['dept_'+this.order_type]!=undefined){
					this.department = this.myconfig['dept_'+this.order_type]
				}
			},
			//选择目标路径
			toGetShelves(){
				this.globalData.toSecPage = 'shelves' //二级页，货架
				uni.navigateTo({
					url: '/pages/shelves/index?flag=add&type=1&from=head&stock_id='+this.to_stock_id,
				})
			},
			//选择源单
			toSerial(flag='') {
				let url = ''
				if(this.order_type == 0){
					//收货取源单
					this.globalData.toSecPage = 'produce' //二级页，入库任务
					url = '/pages/instock/plan?flag=add&process=shouhuo'
				}
				if(this.order_type == 4){
					if(flag=='yk'){
						//质检列表
						this.globalData.toSecPage = 'incheck' //二级页，质检列表
						url = '/pages/instock/pincheck?status=1'
					}else{
						//产品入库取源单
						this.globalData.toSecPage = 'produce' //二级页，入库任务
						url = '/pages/instock/plan?flag=add&process=proin'
					}
				}
				if(this.order_type == 1||this.order_type == 17){
					//上架取源单
					url = '/pages/stockbill/progress_list?flag=add&order_type=0'
				}
				
				if(this.order_type == 2||this.order_type == 5){
					//拣货取源单   投料清单、发货通知单   
					this.globalData.toSecPage = 'material' //二级页，拣货任务，
					url = '/pages/outstock/appmaterial?flag=add&order_type='+this.order_type 
				}
				
				if(this.order_type == 3||this.order_type == 18){
					//出库取源单
					url = '/pages/stockbill/progress_list?flag=add&order_type=2'
				}
				
				if(url!=''){
					uni.showLoading({
						mask:true
					})
					uni.navigateTo({
						url: url,
						success() {
							uni.hideLoading()
						}
					})
				}
			},
			//选择弹窗
			toselect(name) {
				let url = ''
				if(name == 'supplier'){
					//this.globalData.toSecPage = 'kc' //进入二级页
					url = '/pages/select/supplier'
				}
				if(name == 'department'){
					//this.globalData.toSecPage = 'inventory' //进入二级页
					url = '/pages/select/department'
				}
				
				uni.showLoading({mask:true})
				uni.navigateTo({
					url:url,
					delta:1,
					success() {
						uni.hideLoading()
					}
				})
			},
			//操作记录
			toOperlog(){
				uni.showLoading({
					mask:true
				})
				let url = '/pages/stockbill/list?order_type='+this.order_type
				// if(this.order_type==0){
				// 	//得改到源单，采购单数量
				// 	url = '/pages/stockbill/progress_list?order_type=0'
				// }else if(this.order_type==2){
				// 	//得改到源单，领料申请单数量
				// 	url = '/pages/stockbill/progress_list?order_type=2'
				// }
				uni.navigateTo({
					url: url,
					success() {
						uni.hideLoading()
					}
				})
			},
			//复核列表
			toListFh(){
				let url = '/pages/stockbill/listfh?type='+this.order_type
				// if(!this.$Func.checkUserAuth(url)){
				// 	return 
				// }else{
					uni.showLoading({
						mask:true
					})
					
					uni.navigateTo({
						url: url,
						success() {
							uni.hideLoading()
						}
					})
				//}
			},
			clearShelves(){
				//手动清空
				this.to_shelves_name = ''
				this.to_shelves_id = ''
			},
			clearSelect(){
				//手动清空
				this.supplier = {}
				this.supplier.fName = ''
				this.department = {}
				
				//默认部门
				if(this.myconfig['dept_'+this.order_type]!=undefined){
					this.department = this.myconfig['dept_'+this.order_type]
				}else{
					this.department.fName = ''
				}
			},
			//重置表单
			reset() {
				this.init()
				this.clearShelves()
				this.clearSelect()
			}
		}
	}
</script>